package com.ruiben.mall.impl;

import com.ruiben.mall.IPayService;
import com.ruiben.mall.mapper.UserAddressMapper;
import com.ruiben.mall.mapper.UserMapper;
import com.ruiben.mall.mapper.UserOrderMapper;
import com.ruiben.mall.pojo.User;
import com.ruiben.mall.pojo.UserAddress;
import com.ruiben.mall.pojo.UserOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;

@Service
public class PayServiceImpl implements IPayService {


    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 余额支付
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean balancePay(String userId, String orderId) {

        UserOrder userOrder = userOrderMapper.selectByPrimaryKey(orderId);
        //获取用户的收货地址
        userOrder.setOrderstatus((short)2);
        userOrder.setPaytype((short)3);


        int insert = userOrderMapper.updateByPrimaryKeySelective(userOrder);

        User user = userMapper.selectByPrimaryKey(userId);
        BigDecimal subtract = new BigDecimal(user.getUserMoney()).subtract(userOrder.getSumprice());
        user.setUserMoney(subtract.longValue());
        userMapper.updateByPrimaryKey(user);
        return insert>0;

    }
}
